//
//  LayerShadowViewController.m
//  CoreAnimation
//
//  Created by 张国文 on 16/10/9.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "LayerShadowViewController.h"
#import "UIDevice+XJDevice.h"

@interface LayerShadowViewController ()

@property (nonatomic, strong) CALayer *backGrayLayer;
@property (nonatomic, strong) CALayer *firstLayer;
@property (nonatomic, strong) CALayer *secondLayer;
@property (nonatomic, strong) CALayer *firstRedLayer;
@property (nonatomic, strong) CALayer *secondRedLayer;
@property (nonatomic, strong) CALayer *shadowLayer; // 该图层用于secondLayer的外层阴影

@end

@implementation LayerShadowViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    // 默认的阴影形状
//    [self addMySubview];
    // 运用CGPath添加阴影效果
    [self addShadowWithCGPath];
}

- (BOOL)shouldAutorotate
{
    NSLog(@"不允许旋转");
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    NSLog(@"只支持横屏");
    return UIInterfaceOrientationMaskLandscapeLeft;
}

// 默认的阴影形状
- (void)addMySubview
{
    
    
    self.backGrayLayer = [[CALayer alloc] init];
    self.backGrayLayer.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.3].CGColor;
//    self.backGrayLayer.shadowRadius = 10.0;
//    self.backGrayLayer.shadowOpacity = 1.0;
//    self.backGrayLayer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    self.backGrayLayer.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    
    
    [self.view.layer addSublayer:_backGrayLayer];
    self.firstLayer = [[CALayer alloc] init];
    self.firstLayer.frame = CGRectMake((100), 100, 200, 200);
    self.firstLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.firstLayer.cornerRadius = 10;
    self.firstLayer.borderColor = [UIColor blackColor].CGColor;
    self.firstLayer.borderWidth = 5.0f;
    self.firstLayer.shadowOpacity = 0.5;
    self.firstLayer.shadowOffset = CGSizeMake(0, 5.0f);
    self.firstLayer.shadowRadius = 5.0f;
    [self.backGrayLayer addSublayer:_firstLayer];
    
    self.firstRedLayer = [[CALayer alloc] init];
    self.firstRedLayer.frame = CGRectMake(-30, -30, 100, 100);
    self.firstRedLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.firstLayer addSublayer:_firstRedLayer];
    
    // 添加secondLayer的外层视图,注释和关掉，来看看secondLayer有什么变化
    self.shadowLayer = [[CALayer alloc] init];
    self.shadowLayer.frame = CGRectMake(380, 100, 200, 200);
    self.shadowLayer.backgroundColor = [UIColor greenColor].CGColor; // 注意layer必须有背景颜色才会有阴影,不能是透明的颜色
    self.shadowLayer.cornerRadius = 10.0f;
    self.shadowLayer.shadowOpacity = 0.5;
    self.shadowLayer.shadowOffset = CGSizeMake(0, 5.0f);
    self.shadowLayer.shadowRadius = 5.0f;
    [self.backGrayLayer addSublayer:_shadowLayer];

    
    self.secondLayer = [[CALayer alloc] init];
    self.secondLayer.frame = CGRectMake(380, 100, 200, 200);
    self.secondLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.secondLayer.cornerRadius = 10;
    /**
     *  重要：当阴影和裁剪扯上关系的时候就有一个头疼的限制：阴影通常就是在Layer的边界之外，如果你开启了masksToBounds属性，所有从图层中突出来的内容都会被才剪掉
     */
    self.secondLayer.masksToBounds = YES;// 如果开启maskToBounds=YES阴影效果就没有了
    self.secondLayer.borderColor = [UIColor blackColor].CGColor;
    self.secondLayer.borderWidth = 5.0f;
    self.secondLayer.shadowOpacity = 0.5;
    self.secondLayer.shadowOffset = CGSizeMake(0, 5.0f);
    self.secondLayer.shadowRadius = 5.0f;
    [self.backGrayLayer addSublayer:_secondLayer];
    
    self.secondRedLayer = [[CALayer alloc] init];
    self.secondRedLayer.frame = CGRectMake(-30, -30, 100, 100);
    self.secondRedLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.secondLayer addSublayer:_secondRedLayer];
    
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(250, 340, 100, 30);
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    [returnButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnButton];
    
}

// 运用CGPath添加阴影效果
- (void)addShadowWithCGPath
{
    self.backGrayLayer = [[CALayer alloc] init];
    self.backGrayLayer.backgroundColor = [[UIColor grayColor] colorWithAlphaComponent:0.3].CGColor;
    //    self.backGrayLayer.shadowRadius = 10.0;
    //    self.backGrayLayer.shadowOpacity = 1.0;
    //    self.backGrayLayer.shadowOffset = CGSizeMake(0.0f, 5.0f);
    self.backGrayLayer.frame = CGRectMake(0, 0, self.view.frame.size.height, self.view.frame.size.width);
    
    
    [self.view.layer addSublayer:_backGrayLayer];
    self.firstLayer = [[CALayer alloc] init];
    self.firstLayer.frame = CGRectMake((100), 100, 200, 200);
    self.firstLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.firstLayer.cornerRadius = 10;
    self.firstLayer.borderColor = [UIColor blackColor].CGColor;
    self.firstLayer.borderWidth = 5.0f;
    self.firstLayer.shadowOpacity = 0.5;
    self.firstLayer.shadowOffset = CGSizeMake(-25.0f, -25.0f); // 默认向上默认值（0,-3）
    
    self.firstLayer.shadowRadius = 5.0f;
    [self.backGrayLayer addSublayer:_firstLayer];
    
    // 运用CGPath给firstLayer一个圆形的阴影形状
    CGMutablePathRef pathOfFirstLayer = CGPathCreateMutable();
    CGPathAddEllipseInRect(pathOfFirstLayer, NULL, CGRectMake(0, 0, 250, 250));
    self.firstLayer.shadowPath = pathOfFirstLayer;
    
    self.firstRedLayer = [[CALayer alloc] init];
    self.firstRedLayer.frame = CGRectMake(-30, -30, 100, 100);
    self.firstRedLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.firstLayer addSublayer:_firstRedLayer];
    
    // 添加secondLayer的外层视图,注释和关掉，来看看secondLayer有什么变化
    self.shadowLayer = [[CALayer alloc] init];
    self.shadowLayer.frame = CGRectMake(380, 100, 200, 200);
    self.shadowLayer.backgroundColor = [UIColor greenColor].CGColor; // 注意layer必须有背景颜色才会有阴影,不能是透明的颜色
    self.shadowLayer.cornerRadius = 10.0f;
    self.shadowLayer.shadowOpacity = 0.5;
    self.shadowLayer.shadowOffset = CGSizeMake(-25.0f, -25.0f);
    self.shadowLayer.shadowRadius = 5.0f;
   
    [self.backGrayLayer addSublayer:_shadowLayer];
    // 运用CGPath给shadowLayer一个正方形的阴影
    CGMutablePathRef pathOrSecondLayer = CGPathCreateMutable();
    CGPathAddRect(pathOrSecondLayer, NULL, CGRectMake(0, 0, 250, 250));
    self.shadowLayer.shadowPath = pathOrSecondLayer;
    
    self.secondLayer = [[CALayer alloc] init];
    self.secondLayer.frame = CGRectMake(380, 100, 200, 200);
    self.secondLayer.backgroundColor = [UIColor whiteColor].CGColor;
    self.secondLayer.cornerRadius = 10;
    /**
     *  重要：当阴影和裁剪扯上关系的时候就有一个头疼的限制：阴影通常就是在Layer的边界之外，如果你开启了masksToBounds属性，所有从图层中突出来的内容都会被才剪掉
     */
    self.secondLayer.masksToBounds = YES;// 如果开启maskToBounds=YES阴影效果就没有了
    self.secondLayer.borderColor = [UIColor blackColor].CGColor;
    self.secondLayer.borderWidth = 5.0f;
    self.secondLayer.shadowOpacity = 0.5;
    [self.backGrayLayer addSublayer:_secondLayer];
    
    self.secondRedLayer = [[CALayer alloc] init];
    self.secondRedLayer.frame = CGRectMake(-30, -30, 100, 100);
    self.secondRedLayer.backgroundColor = [UIColor redColor].CGColor;
    [self.secondLayer addSublayer:_secondRedLayer];
    
    
    
    
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    returnButton.frame = CGRectMake(250, 340, 100, 30);
    [returnButton setTitle:@"返回" forState:UIControlStateNormal];
    [returnButton setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
    [returnButton addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:returnButton];

}

- (void)returnButton:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [UIDevice setOrientation:UIInterfaceOrientationPortrait];
}

@end
